//
//  PresentAnimation.m
//  PayViewDemo
//
//  Created by _Finder丶Tiwk on 15/6/11.
//  Copyright (c) 2015年 _Finder丶Tiwk. All rights reserved.
//

#import "PresentAnimation.h"
#import "UIView+Add.h"

@implementation PresentAnimation


+(instancetype)animationByPresentType:(KPresentAnimationType)presentType{
    PresentAnimation *animation = [[PresentAnimation alloc] init];
    animation.animationType = presentType;
    return animation;
}

-(NSTimeInterval)animationDuration{
    return (_animationDuration==0)?0.6:_animationDuration;
}

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return self.animationDuration;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *origin = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *destination = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView =[transitionContext containerView];
    UIImage *bgImg = [origin.view snapshotImage];
    containerView.backgroundColor =[UIColor colorWithPatternImage:bgImg];
    
//    UIView *backgroundView = [[UIView alloc] initWithFrame:containerView.bounds];
//    backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
//    [containerView addSubview:backgroundView];
    
    [containerView addSubview:destination.view];
    
     CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect finalFrame = [transitionContext finalFrameForViewController:destination];
    
    NSTimeInterval duration =[self transitionDuration:transitionContext];
    
    switch (self.animationType) {
        case PresentSlideFromRight:
        {
            destination.view.frame = CGRectOffset(finalFrame, screenBounds.size.width,0);
            destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:1];
            [UIView animateWithDuration:duration
                                  delay:0
                 usingSpringWithDamping:0.6
                  initialSpringVelocity:0
                                options:UIViewAnimationOptionCurveEaseIn
                             animations:^{
                                 destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
                                 destination.view.frame = finalFrame;
                             } completion:^(BOOL finished) {
                                 [transitionContext completeTransition:YES];
                             }];
        }
            break;
        case PresentScaleFromSmallToBig:
        {
            destination.view.transform = CGAffineTransformMakeScale(0.2, 0.2);
            destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
            
            [UIView animateWithDuration:duration animations:^{
                destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
                destination.view.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
            
        }
            break;
        case PresentEaseIn:
        {
            destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
            
            [UIView animateWithDuration:duration animations:^{
                destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
            
        }
            break;
            
        case PrensentSlideFromBottom:
        {
            destination.view.frame = CGRectOffset(finalFrame, 0,screenBounds.size.height);
//            destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:1];
            [UIView animateWithDuration:duration
                                  delay:0
                 usingSpringWithDamping:1
                  initialSpringVelocity:0
                                options:UIViewAnimationOptionCurveEaseIn
                             animations:^{
                                 destination.view.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
                                 destination.view.frame = finalFrame;
                             } completion:^(BOOL finished) {
                                 [transitionContext completeTransition:YES];

                             }];
            
        }
            break;
            
        default:
            break;
    }
}

@end
